#import <Foundation/Foundation.h>
#import "plugin_result.h"

@implementation PluginResult

NSMutableDictionary* respMap;

+ (PluginResult*)create:(FlutterResult)result {
    PluginResult* r = [PluginResult alloc];
    r = [r initWithResult:result];
    return r;
}

- (instancetype)initWithResult:(FlutterResult)result;
{
    self = [super init];
    if (self) {
        respMap = [[NSMutableDictionary alloc] init];
        self.result = result;
    }
    return self;
}
 
- (void)sendOk
{
    [self sendCode:CODE_OK msg:@""];
}

- (void)sendUnkonwError:(NSString*) msg
{
    [self sendCode:CODE_UNKOWN_ERROR msg:msg];
}

- (void)sendCancel:(NSString*) msg
{
    [self sendCode:CODE_CANCEL msg:msg];
}


- (void)sendInvalidedArgs:(NSString*) msg
{
    [self sendCode:CODE_INVALIDED_ARGS msg:msg];
}

- (void)sendCode:(NSInteger) code msg:(NSString*) msg
{
    //check ui thread
    //check result is valide
    [respMap setValue:[NSNumber numberWithInteger:code] forKey:@"code"];
    [respMap setValue:msg forKey:@"msg"];
    self.result(respMap);
}


- (void)setValue:( NSObject*)value forKey:(NSString *)key{
    [respMap setValue:value forKey:key];
}

@end


